package org.boot.ssm.wxshop.web.controller;

import com.github.pagehelper.PageInfo;
import org.boot.ssm.wxshop.domain.ShopCar;
import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.service.ShopCarService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @program: wxshop
 * @description: 购物车控制层
 * @author: Parasomnia
 * @create: 2018-03-16 00:42
 **/
@Controller
@RequestMapping("/shopcar")
public class ShopCarController {
    @Autowired
    private ShopCarService shopCarService;

    /**
     * 去购物车
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/front/myCar")
    public String myCar(HttpSession session, Model model){
        Object obj = session.getAttribute("user");
        if(obj != null && obj instanceof User){
            User user = (User)obj;
            ShopCar car = new ShopCar();
            car.setIsDelete(1);
            car.setUserId(user.getId());
            model.addAttribute("carList",shopCarService.queryParamList(car));
            return "frontstage/shopcar/shopcart";
        }
        throw new RuntimeException("请先登录");
    }

    /**
     * 删除购物车
     * @param shopCar 购物车对象
     * @return
     */
    @RequestMapping("/front/delcar")
    @ResponseBody
    public JsonResult delCar(ShopCar shopCar){
        shopCar.setIsDelete(0);
        shopCarService.updateDelete(shopCar);
        return new JsonResult(0,"操作成功");
    }

    /**
     * 修改购物车中的个数
     * @param shopCar
     * @return
     */
    @RequestMapping("/front/updateNum")
    @ResponseBody
    public JsonResult updateNum(ShopCar shopCar){
        Integer num = shopCar.getNum();
        shopCar = shopCarService.queryById(shopCar.getId());
        shopCar.setCountMoney(shopCar.getPrice()*num);
        shopCar.setNum(num);
        shopCarService.updateNoNull(shopCar);
        return new JsonResult(0,"操作成功");
    }

    /**
     * 添加购物车
     * @param shopCar
     * @param session
     * @return
     */
    @RequestMapping("/front/addCar")
    @ResponseBody
    public JsonResult addCar(ShopCar shopCar,HttpSession session){
        Object obj = session.getAttribute("user");
        if(obj!=null){
            User user = (User)obj;
            shopCarService.addCar(shopCar,user.getId());
        }
        return new JsonResult(0,"添加成功",shopCar);
    }






    //=============================================后台==============================================================================


    /**
     * 去购物车查询页面
     * @return
     */
    @RequestMapping("/goShopCarmgr")
    public String shopCarMgr(){
        return "backstage/shopcar/mgr";
    }

    /**
     * 分页根据用户id查询购物车
     * @param page
     * @param limit
     * @param shopCar
     * @return
     */
    @RequestMapping("/shopCarList")
    @ResponseBody
    public JsonResult shopCarList(Integer page,Integer limit,ShopCar shopCar){
        shopCar.setIsDelete(1);
        PageInfo<ShopCar> pageInfo = shopCarService.queryPageListByParam(page,limit,shopCar);
        return new JsonResult(0,"加载成功",pageInfo.getList(),pageInfo.getTotal());
    }
}
